package org.easymis.crm.modules.biplatform.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.easymis.commons.web.utils.Page;
import org.easymis.commons.web.utils.R;
import org.easymis.crm.modules.biplatform.entitys.mybatis.dto.Card;
import org.easymis.crm.modules.biplatform.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


@Api(tags = "数据卡片")
@Controller
public class CardController {
	@Autowired
	private CardService service;
	@ApiOperation(value = "跳转添加业务切片", notes = "", produces = MediaType.TEXT_HTML_VALUE)
	@RequestMapping(value = { "/card/forward/add.html" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String forwardAdd(ModelMap map, Page page) {

		return "/biplatform/card/add";
	}
	@ApiOperation(value = "保存切片信息", notes = "", produces = MediaType.TEXT_HTML_VALUE)
	@RequestMapping(value = { "/card/save.do" }, method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public R save(ModelMap map, Card bean) {
		service.save(bean);
		return new R().ok();
	}
	@ApiOperation(value = "跳转添加业务切片列表", notes = "", produces = MediaType.TEXT_HTML_VALUE)
	@RequestMapping(value = { "/card/forward/list.html" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String forwardList(ModelMap map, Page page) {
		PageHelper.startPage(page.getCurPage(), page.getPageSize());
		List<Card> dashboardList = service.getList(new HashMap<String, Object>());
		PageInfo<Card> p = new PageInfo<Card>(dashboardList);
		return "/biplatform/card/list";
	}
	@ApiOperation(value = "业务切片列表json数据", notes = "", produces = MediaType.TEXT_HTML_VALUE)
	@RequestMapping(value = { "/card/forward/list.json" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String forwardListJson(ModelMap map, Page page) {
		PageHelper.startPage(page.getCurPage(), page.getPageSize());
		List<Card> dashboardList = service.getList(new HashMap<String, Object>());
		PageInfo<Card> p = new PageInfo<Card>(dashboardList);
		return "/biplatform/card/list";
	}
	@ApiOperation(value = "数据卡片列表", notes = "", produces = MediaType.TEXT_HTML_VALUE)
	@RequestMapping(value = { "/api/card/query" }, method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public R apiCardFind(ModelMap map, Page page) {
		PageHelper.startPage(page.getCurPage(), page.getPageSize());
		List<Card> dashboardList = service.getList(new HashMap<String, Object>());
		PageInfo<Card> p = new PageInfo<Card>(dashboardList);
		return new R().put("page", p);
	}
	@ApiOperation(value = "数据卡片入口", notes = "html", produces = MediaType.TEXT_HTML_VALUE)
	@RequestMapping(value = { "/api/card/{cardId}.html" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String apiCard(ModelMap map, HttpServletRequest request,HttpServletResponse response,@PathVariable("cardId") String cardId,
			String datasource_type,String datasource_id) {
		Boolean csv=false;//(request.getParameter("csv")==null):false;
		Boolean query =false;
		Boolean force =false;
		String formData=request.getParameter("form_data");
		String datasourceType = null;
		String datasourceId = null;
		Map jsonMap=service.generateJson(datasourceType, datasourceId, formData, csv, query, force);
		return "/biplatform/card/view";
	}
	
	@ApiOperation(value = "数据卡片入口", notes = "json", produces = MediaType.TEXT_HTML_VALUE)
	@RequestMapping(value = { "/api/card/{cardId}.json" }, method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public R apiCardJson(ModelMap map, HttpServletRequest request,HttpServletResponse response,@PathVariable("cardId") String cardId,
			String datasource_type,String datasource_id) {
		Boolean csv=false;//(request.getParameter("csv")==null):false;
		Boolean query =false;
		Boolean force =false;
		String formData=request.getParameter("form_data");
		String datasourceType = null;
		String datasourceId = null;
		Map jsonMap=service.generateJson(datasourceType, datasourceId, formData, csv, query, force);
		return new R().put("json", jsonMap);
	}
}
